<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validateTrigger="bind">
      <uni-forms-item name="barcode" label="" required>
        <uni-easyinput placeholder="条形码" v-model="formData.barcode" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="" required>
        <uni-easyinput placeholder="姓名" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="sex" label="" required>
        <uni-easyinput placeholder="性别" v-model="formData.sex" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="birthday" label="" required>
        <uni-easyinput placeholder="生日" v-model="formData.birthday" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="phone" label="" required>
        <uni-easyinput placeholder="手机号" v-model="formData.phone" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="result" label="" required>
        <uni-easyinput placeholder="结果" v-model="formData.result" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="is_deleted" label="">
        <switch @change="binddata('is_deleted', $event.detail.value)" :checked="formData.is_deleted"></switch>
      </uni-forms-item>
      <uni-forms-item name="creator" label="">
        <uni-easyinput placeholder="创建人" v-model="formData.creator"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="updator" label="">
        <uni-easyinput placeholder="修改人" v-model="formData.updator"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="update_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.update_time"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/exam-result.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'exam-result';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
    data() {
      let formData = {
        "barcode": "",
        "name": "",
        "sex": "",
        "birthday": "",
        "phone": "",
        "result": "",
        "is_deleted": false,
        "creator": "",
        "create_time": null,
        "updator": "",
        "update_time": null
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("barcode,name,sex,birthday,phone,result,is_deleted,creator,create_time,updator,update_time").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
